<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item :label="$t('排序序号')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sort">
              <a-input-number v-model="model.sort" :placeholder="$t('请输入排序序号')" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('频道名称')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" :placeholder="$t('请输入')" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('频道类型')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="channel">
              <j-dict-select-tag
                type="list"
                v-model="model.channel"
                dictCode="bis_applet_channel.channel"
                :placeholder="$t('请选择')"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('状态')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="state">
              <j-dict-select-tag
                type="list"
                v-model="model.state"
                dictCode="bis_coupon_set.state"
                :placeholder="$t('请选择状态')"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.channel == '6'">
            <a-form-model-item :label="$t('跳转页面')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jumpType">
              <j-dict-select-tag
                type="list"
                v-model="model.jumpType"
                dictCode="bis_applet_banner.jump_type"
                :placeholder="$t('请选择跳转页面')"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.jumpType == '8'">
            <a-form-model-item :label="$t('跳转地址')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jumpUrl">
              <a-input
                v-model="model.jumpUrl"
                :placeholder="$t('请输入跳转地址')"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('说明')" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea v-model="model.intro" :row="2" :disabled="disabled"></a-textarea>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('频道图标')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="picture" :required="model.channel == '6'">
              <div style="display: flex">
                <j-image-upload v-model="model.picture"></j-image-upload>
                <span style="color: orange; padding-top: 30px; padding-left: 20px">{{
                  $t('用于小程序展示，建议尺寸：宽710*高272px')
                }}</span>
              </div>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'BisAppletChannelForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        sort: [{ required: true, message: this.$t('请输入排序序号') }],
        name: [{ required: true, message: this.$t('请输入频道名称') }],
        channel: [{ required: true, message: this.$t('请选择') }],
        state: [{ required: true, message: this.$t('请输入状态') }],
        jumpType: [{ required: true, message: this.$t('请选择跳转页面') }],
        jumpUrl: [{ required: true, message: this.$t('请输入跳转地址') }],
      },
      url: {
        add: '/applet/bisAppletChannel/add',
        edit: '/applet/bisAppletChannel/edit',
        queryById: '/applet/bisAppletChannel/queryById',
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add(record) {
      this.edit(Object.assign({}, this.modelDefault, record))
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(()=>{
        if(record.channel == '6'){
          this.validatorRules.picture = [{ required: true, message: this.$t('请输入频道图标') }]
        } else {
          this.validatorRules.picture = [{ required: false, message: this.$t('请输入频道图标') }]
        }
      })
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>